package com.apimarket.advice;


import com.apimarket.domain.ExceptionResult;
import com.apimarket.exception.AMException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * xuyang
 * 自动拦截所有的Controller
 */
@ControllerAdvice
public class CommonExceptionHandler {

    /**
     * 处理异常，返回值：要返回到页面的信息
     * 注解：处理运行时异常{Exceptions handled by the annotated method. If empty, will default to any
     * exceptions listed in the method argument list.}
     * @param e
     * @return
     */
    @ExceptionHandler(AMException.class)
    public ResponseEntity<ExceptionResult> responseEntity(AMException e){
        return ResponseEntity.status(e.getExceptionEnum().getCode())
                .body(new ExceptionResult(e.getExceptionEnum()));
    }
}
